package controllers;

import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.JOptionPane;

import models.CustomerModel;
import models.CustomerPurchaseModel;

import dialogs.InsertTupleDialog;

import system.View;

public class CustomerRegistrationController extends AbstractController {
	private CustomerModel customer = new CustomerModel();

	public CustomerRegistrationController(View v) {
		super(v);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("Register"))
			showDialogWindow(new InsertTupleDialog(view, actionCommand,
					"Customer", new String[] { "Customer ID", "Customer Password: ", "Name: ",
							"Address: ", "Phone Number: " }, this));
	}

	@Override
	public int validateInsert(Vector<String> s) {
		if(s.size() != 5) return VALIDATIONERROR;
		
		try 
		{	
			String cid;
			String password;
			String name;
			String address;
			String phone;
			
			if(s.get(0).trim().length() != 0)
				cid = s.get(0).trim();
			else return VALIDATIONERROR;
			if(s.get(1).trim().length() != 0)
				password = s.get(1).trim();
			else return VALIDATIONERROR;
			if(s.get(2).length() != 0)
				name = s.get(2).trim();
			else return VALIDATIONERROR;
			if(s.get(3).length() != 0)
				address = s.get(3).trim();			
			else return VALIDATIONERROR;
			if(s.get(4).length() != 0)
				phone = s.get(4).trim();
			else return VALIDATIONERROR;
			
			view.updateStatusBar("Inserting Customer...");

			if ( customer.insert(cid, password, name, address, phone) ) {
				CustomerPurchaseModel.setCID(cid);
				JOptionPane.showMessageDialog(view, "You are now logged in", "Registration is Complete", JOptionPane.INFORMATION_MESSAGE);
				return operationSuccess();
			}
			else{
				JOptionPane.showMessageDialog(view, "Customer Id must be unique", "Please retry", JOptionPane.ERROR_MESSAGE);
				return operationFailure(); 
			}
		}  catch (NumberFormatException ex) { return VALIDATIONERROR; }
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
	}

	@Override
	public int validateDelete(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}
}
